﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Sims3Package.Wrapper
{
    public class ROCL
    {
        public int Version { get; set; }
        public TGIBlockList InternalChunk { get; set; }
        public TGIBlockList ExternalChunk { get; set; }
        public List<Tuple<int, int>> InternalChunkInfo {get;set;}
        public int Index3 { get; set; }

        public ROCL(Stream s)
        {
            BinaryReader r = new BinaryReader(s);
            this.Version = r.ReadInt32();
            int internalpubliccount = r.ReadInt32();
            this.Index3 = r.ReadInt32();
            int externalcount = r.ReadInt32();
            int internalcount = r.ReadInt32();
            this.InternalChunk = new TGIBlockList(internalpubliccount);
            for (int i = 0; i < internalpubliccount; i++) this.InternalChunk.Add(new TGIBlock(r, TGIOrder.ITG));
            this.ExternalChunk = new TGIBlockList(externalcount);
            for (int i = 0; i < externalcount; i++) this.ExternalChunk.Add(new TGIBlock(r, TGIOrder.ITG));
            this.InternalChunkInfo = new List<Tuple<int, int>>(internalcount);
            for (int i = 0; i < internalcount; i++) this.InternalChunkInfo.Add(new Tuple<int, int>(r.ReadInt32(), r.ReadInt32()));
        }

        public string Value
        {
            get
            {
                StringBuilder b = new StringBuilder();
                b.AppendFormat("ROCL Chunk  Version: 0x{0:X8}", this.Version).AppendLine();
                b.AppendFormat("Index3: 0x{0:X8}\nInternal Chunks: Count 0x{1:X8}\n", this.Index3, this.InternalChunk.Count);
                for (int i = 0; i < this.InternalChunk.Count; i++) b.AppendFormat("  [{0:X2}]: {1}\n", i, this.InternalChunk[i]);
                b.AppendFormat("External Chunks: Count 0x{0:X8}\n", this.ExternalChunk.Count);
                for (int i = 0; i < this.ExternalChunk.Count; i++) b.AppendFormat("  [{0:X2}]: {1}\n", i, this.ExternalChunk[i]);
                b.AppendFormat("Chunk Info: Count 0x{0:X8}\n", this.InternalChunkInfo.Count);
                for (int i = 0; i < this.InternalChunkInfo.Count; i++) b.AppendFormat("  [{0:X2}]: Position: 0x{1:X8} Size: 0x{2:X8}\n", i, this.InternalChunkInfo[i].Item1, this.InternalChunkInfo[i].Item2);
                return b.ToString();
            }
        }

        public void UnParse(Stream s)
        {
            BinaryWriter w = new BinaryWriter(s);
            w.Write(this.Version);
            w.Write(this.InternalChunk.Count);
            w.Write(this.Index3);
            w.Write(ExternalChunk.Count);
            w.Write(InternalChunkInfo.Count);
            foreach (TGIBlock tgi in this.InternalChunk) tgi.UnParse(s);
            foreach (TGIBlock tgi in this.ExternalChunk) tgi.UnParse(s);
            foreach (var tuple in this.InternalChunkInfo) { w.Write(tuple.Item1); w.Write(tuple.Item2); }
        }


    }
}
